<template>
  <div>
    <!-- 头部 -->
    <market-header></market-header>
    <!-- 内容 -->
    <div class="container">
      <div class="input">
        <label>用户登录</label>
        <rh-input v-model="username"
                  placeholder="请输入用户名"></rh-input>
      </div>
      <div class="input">
        <label>用户密码</label>
        <rh-input type='password'
                  v-model="password"
                  placeholder="请输入密码"></rh-input>
      </div>
      <div class="input check">
        <label>验证码</label>
        <rh-input v-model="check"
                  placeholder="请输入验证码"></rh-input>
        <canvas width="150"
                height="40"
                ref="canvas"
                @click="refresh"></canvas>
      </div>
      <div class="input">
        <rh-button type='f30'
                   @click="userLogin">登录</rh-button>
        <router-link to="/register">
          <rh-button type='f30'>立即注册</rh-button>
        </router-link>
      </div>
    </div>
    <!-- 尾部 -->
    <market-footer></market-footer>
  </div>
</template>

<script>
import MarketHeader from "@/components/common/MarketHeader"
import MarketFooter from "@/components/common/MarketFooter"
import RhInput from '@/components/basic/rh-input'
import RhButton from '@/components/basic/rh-button'
export default {
  components: {
    MarketHeader,
    MarketFooter,
    RhInput,
    RhButton
  },
  data () {
    return {
      username: "",
      password: "",
      check: '',
      checkarr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
      str: ''
    }
  },
  mounted () {
    this.showCheck();
  },
  methods: {
    userLogin () {
      if (this.check !== this.str) {
        alert('验证码错误')
        this.showCheck();
        return;
      }
      this.$http.post('http://localhost:3000/users/login', { username: this.username, password: this.password }).then(response => {
        if (response.data.status == 1) {
          alert(response.data.message);
        } else {
          let save_token = {
            token: response.data.data.token,
            username: this.username
          }
          localStorage.setItem('token', response.data.data.token);
          localStorage.setItem('username', response.data.data.user.username);
          localStorage.setItem('_id', response.data.data.user._id);
          this.$router.push({ path: '/' });
        }
      })
    },
    showCheck () {
      const c = this.$refs.canvas,
        ctx = c.getContext('2d');
      this.str = '';
      for (let i = 0; i < 6; i++) {
        this.str += this.checkarr[Math.floor(Math.random() * 10)];
      }
      ctx.fillStyle = 'pink';
      ctx.fillRect(0, 0, 150, 40);
      ctx.fillStyle = 'black';
      ctx.font = '20px Roboto Slab';
      ctx.textAlign = 'center';
      ctx.setTransform(1, -0.12, 0.3, 1, 0, 12);
      ctx.fillText(this.str, 75, 25);
    },
    refresh () {
      this.showCheck();
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  margin: 100px auto;
  width: 400px;
  height: 300px;
  border: 1px solid #ccc;

  .input {
    margin-top: 22px;
  }
  .rh-input {
    margin-left: 80px;
    width: 300px;
  }
  .rh-button {
    width: 142px;
  }
  .input > .rh-button:nth-of-type(1) {
    margin-left: 80px;
  }
  label {
    width: 80px;
    height: 40px;
    text-align: right;
    vertical-align: middle;
    float: left;
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    box-sizing: border-box;
  }
  a {
    text-decoration: none;
    color: #fff;
  }
  .check {
    display: flex;
    .rh-input {
      width: 140px;
      margin: 0;
    }
    canvas {
      cursor: pointer;
      margin-left: 8px;
    }
  }
}
</style>